/*
 * @Date: 2022-04-06 10:32:28
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-08-05 09:07:44
 * @FilePath: \imitation-microblog-vue\src\router\index.ts
 * @Description: 路由配置
 */

import {
  createWebHistory,
  RouteRecordRaw,
  createRouter,
  createWebHashHistory,
} from "vue-router";
import store from "../store/index";
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "Home",
    component: () => import("../views/Home/index.vue"),
    redirect: "/weibohome",
    children: [
      {
        path: "/setting",
        name: "Setting",
        component: () => import("../views/Setting/index.vue"),
      },
      {
        path: "/weibohome",
        name: "weiBoHome",
        component: () => import("../views/weiboHome/index.vue"),
      },
      {
        path: "/personal/:id",
        name: "Personal",
        component: () => import("../views/personal/index.vue"),
      },
      //关注界面
      {
        path: "/weibohome/following/:id",
        name: "Following",
        component: () => import("../views/FollowListShow/index.vue"),
      },
      //粉丝界面
      {
        path: "/weibohome/fans/:id",
        name: "Fans",
        component: () => import("../views/fansListShow/index.vue"),
      },
      //@详情界面
      {
        path: "/atMeDetail",
        name: "atMeDetail",
        component: () => import("../views/showAtMeDetail/index.vue"),
      },
    ],
  },
  {
    path: "/error",
    name: "NOTFOUND",
    component: () => import("../views/error404/index.vue"),
  },
  //登陆
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login/Login.vue"),
  },
  //注册
  {
    path: "/regsiter",
    name: "Regsiter",
    component: () => import("../views/Regsiter/index.vue"),
  },
  //博客详情
  {
    path: "/blogdetail/:blogId/:userId",
    name: "blogDetail",
    component: () => import("../views/blogDetail/index.vue"),
  },
];
const router = createRouter({
  routes,
  history: createWebHistory(),
  scrollBehavior(to, from, savedPosition) {
    //第一次打开显示吸顶
    if (
      to.fullPath == "/weibohome" &&
      from.fullPath === "/" &&
      savedPosition === null
    ) {
      return { top: 0 };
    } else {
      if (savedPosition) {
        return savedPosition;
      } else {
        return { top: 910 };
      }
    }
  },
});
router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {
    next({ name: "NOTFOUND" });
  } else {
    if (to.fullPath == "/login" || to.fullPath == "/regsiter") {
      if (localStorage.getItem("isLogin") == "1") {
        // console.log(localStorage.getItem("isLogin"));
        next({ name: "Home" });
      } else {
        next(); //如果匹配到正确跳转
      }
    } else {
      next(); //如果匹配到正确跳转
    }
  }
});
export default router;
